package com.fwtai.tool;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * 图片处理
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2021/12/8 10:03
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
public final class ToolImage{

  private ToolImage(){}

  public static void scale(final String pathOriginal,final String pathTarget){
    try {
      Thumbnails.of(new File(pathOriginal))
        .scale(0.50)//缩放
        .toFile(new File(pathTarget));
    } catch (final IOException ignored){}
  }

  //判断是否为空再处理业务
  public static HashMap<String,Integer> getWidthHeight(final String filePath){
    try {
      final FileInputStream inputStream = new FileInputStream(filePath);
      final BufferedImage image = ImageIO.read(inputStream);
      final int width = image.getWidth();
      final int height = image.getHeight();
      final HashMap<String,Integer> map = new HashMap<>(2);
      map.put("width",width);
      map.put("height",height);
      inputStream.close();
      return map;
    } catch (final Exception ignored){
      return null;
    }
  }

  /**
   * 指定路径压缩大中小图
   * @param type 1: 800x480 大图路径；2: 480x320 中图路径；3: 320x240 小图路径
   * @param formatName 图片的格式 jpeg|png|jpg
   * @作者 田应平
   * @QQ 444141300
   * @创建时间 2019/10/11 19:34
  */
  public static boolean compress(final ThreadPoolTaskExecutor executor,final String originalPath,final String targetPath,final int type,final String formatName){
    try {
      switch (type){
        case 1:// 800x480 大图路径
          return compress(executor,originalPath,targetPath,formatName,800,480);
        case 2:// 480x320 中图路径
          return compress(executor,originalPath,targetPath,formatName,480,320);
        case 3:// 320x240 小图路径
          return compress(executor,originalPath,targetPath,formatName,320,240);
        case 4:
          return compress(executor,originalPath,targetPath,formatName,64,64);// 320x240 小图路径
        default:
          return false;
      }
    } catch (final Exception e){
      return false;
    }
  }

  public static boolean compress(final ThreadPoolTaskExecutor executor,final String originalPath,final String targetPath,final String formatName,final int width,final int height){
    executor.execute(()-> {
      try{
        resize(originalPath,targetPath,formatName,width,height);
      }catch(final IOException e){
        throw new RuntimeException(e);
      }
    });
    return true;
  }

  private static void resize(final String originalPath,final String targetPath,final String formatName,final int width,final int height) throws IOException{
    final File targetfile = new File(targetPath);
    if(!targetfile.exists()){
      targetfile.getParentFile().mkdirs();
    }
    final BufferedImage im = ImageIO.read(new File(originalPath));
    // 生成新的图片
    final BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    image.getGraphics().drawImage(im.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
    ImageIO.write(image,formatName, new File(targetPath));//无损输出图片到指定路径
    image.flush();
  }

  public static void compress(final String pathOriginal,final String pathTarget){
    try {
      Thumbnails.of(new File(pathOriginal))
        .size(1024,1024)
        .outputQuality(0.56)//压缩质量
        .toFile(new File(pathTarget));
    } catch (final IOException ignored){}
  }

  public static void compress(final String pathOriginal,final String pathTarget,final int width,final int height){
    try {
      Thumbnails.of(new File(pathOriginal))
        .size(width,height)//尺寸
        .outputQuality(0.56)
        .toFile(new File(pathTarget));
    } catch (final IOException ignored){}
  }

  /**
   * 是否大于指定像素尺寸
   * @param filePath 图片的全路径
   * @作者 田应平
   * @QQ 444141300
   * @创建时间 2021/12/8 16:08
  */
  public static boolean stipulate(final String filePath,final int width,final int height){
    final File file = new File(filePath);
    final boolean b = file.exists() && file.isFile();
    if(!b) return false;
    try {
      final BufferedImage bi = ImageIO.read(file);
      final int w = bi.getWidth();
      final int h = bi.getHeight();
      return width * height > w * h;
    } catch (final IOException ignored) {}
    return false;
  }
}